<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\system\SysConfigModel;
use think\Request;

/**
 * 系统配置
 * Class SysConfig
 * @package app\controller
 */
class SysConfig extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new SysConfigModel();
    }


    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo()
    {
        $config = $this->model->getInfo(1);
        if (!empty($config)) {
            unset($config['id']);
            return self::returnMsg(1, $config);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => 1,
            'order_time' => $request->param('order_time'),
            'tx_service_key' => $request->param('tx_service_key'),
            'base_benefit' => $request->param('base_benefit'),//默认基础基金
            'pay_integral' => $request->param('pay_integral'),
            'anniversary' => $request->param('anniversary'),//周年庆
            'integral_rule' => $request->param('integral_rule'),
            'user_earnings_rule' => $request->param('user_earnings_rule'),
            'agency_earnings_rule' => $request->param('agency_earnings_rule')
        ];
        $formData['deposit'] = empty($formData['deposit']) ? 0 : $formData['deposit'];
        $validate = 'app\validate\SysConfigValidate';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

}
